#include "app_rtthread.h"
#include "adc_config.h"
#include "adc.h"

#define ADC_DEV_NAME        "adc1" 
#define ADC_DEV_CHANNEL     (5)
#define REFER_VOLTAGE       330         /* 参考电压 3.3V,数据精度乘以100保留2位小数*/
#define CONVERT_BITS        (1 << 12)   /* 转换位数为12位 */

int kisadc(void)
{
   stm32_adc_init();
   rt_uint32_t value, vol;
   rt_err_t ret = RT_EOK;

    rt_adc_device_t adc_dev;           
   
    adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
    if (adc_dev == RT_NULL)
    {
        rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
        return RT_ERROR;
    }
    
    rt_adc_enable(adc_dev,ADC_DEV_CHANNEL);   
    while(1)
    {
        value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
        rt_kprintf("the value is :%d \n", value);

        vol = value * REFER_VOLTAGE / CONVERT_BITS;
        rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
        rt_thread_mdelay(1000);
    }
    
    
    /* 关闭通道 */
    ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);

    return ret;
}

MSH_CMD_EXPORT(kisadc, this is a test adc);

